/**
 * Created by WT-SUN on 2017/4/11.
 */
$(function () {
    $('#pointTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/monPoint/selectPointTb.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'monitorPoint', title: '监测点名称', width: 100},
            {field: 'factorNames', title: '监测项目', width: 100,formatter: function (value, row, index) {
                var name = '';
                if (row.pointHarmIds && row.pointHarmIds.length > 0) {
                    for (var index in row.pointHarmIds) {
                        if (row.pointHarmIds[index].name != null) {
                            name += ',' + row.pointHarmIds[index].name;
                        }
                    }
                    return name.substring(1);
                } else {
                    return '';
                }
            }},
            {field: 'harmNum', title: '监测项目数量', width: 100},
            {field: 'companyName', title: '单位名称', width: 100},
            {field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });
    queryComboData();
    $('#btn_point_add').on('click',function () {
        addPoint();
    });
    $('#btn_point_delete').on('click',function () {
        deletePoint();
    });
    $('#btn_point_edit').on('click',function () {
        editPoint();
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm').form('reset');
    });
});

function queryComboData() {
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_HARM_TYPE;
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $('#harmIdQuery').combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#pointTb').datagrid('clearSelections');
    var queryParams = $('#queryForm').serializeJson();
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#pointTb').datagrid('options').queryParams = queryParams;
    $("#pointTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/monPoint/selectPointTb.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['600px', '60%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addPoint() {
    var title = '新增监测点';
    var content = ROOT_PATH + '/occupationalHealth/monPoint/add.shtml';
    openLayers(title, content);
}
/**
 * 删除
 */
function deletePoint() {
    var rows = $('#pointTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/monPoint/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#pointTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editPoint() {
    var rows = $('#pointTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑监测点';
    var content = ROOT_PATH + '/occupationalHealth/monPoint/info.shtml?id=' + id+'&type=edit';
    openLayers(title, content);
}
